import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreAddonService extends BaseService<any> {
  private readonly logger = new Logger(CoreAddonService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getInitList
   * 对应 PHP: CoreAddonService_core::getInitList()
   * 逻辑类型: undefined - undefined
   */
  async getInitList() {
    // 基于PHP真实逻辑: getInitList
    // PHP原文: return [             'type_list' => AddonDict::getType()         ];     }      /**      * 获取已下载的插件      * @return array      */     public function ge...
return [
            type_list: AddonDict.getType()
        ];
    }

    /**
     * 获取已下载的插件
     * @return */
    async getLocalAddonList()
    {
        const list = [];
        const online_app_list = [];
        const install_addon_list = this.model.append(.status_name).column('title, icon, key, desc, status, author, version, install_time, update_time, cover', 'key');
        try {
            const niucloud_module_list = this.coreModuleService.getModuleList().data || [];
            foreach (niucloud_module_list as v) {
                const data = [
                    title: v.app.app_name,
                    desc: v.app.app_desc,
                    key: v.app.app_key || '',
                    version: v.version || '',
                    author: v.site_name,
                    type: v.app.app_type,
                    support_app: v.app.support_channel || [],
                    is_download: false,
                    is_local: false,
                    icon: v.app.app_logo,
                    cover: v.app.window_logo[0),
                ];
                data.install_info = install_addon_list[v.app.app_key] || [];
                list[v.app.app_key] = data;
            }
            const online_app_list = array_column(list, 'key');
  }

  /**
   * getLocalAddonList
   * 对应 PHP: CoreAddonService_core::getLocalAddonList()
   * 逻辑类型: undefined - undefined
   */
  async getLocalAddonList() {
    // 基于PHP真实逻辑: getLocalAddonList
    // PHP原文: $list = [];         $online_app_list = [];         $install_addon_list = $this->model->append(['status_name'])->column('title, icon, key, desc, status...
const list = [];
        const online_app_list = [];
        const install_addon_list = this.model.append(.status_name).column('title, icon, key, desc, status, author, version, install_time, update_time, cover', 'key');
        try {
            const niucloud_module_list = this.coreModuleService.getModuleList().data || [];
            foreach (niucloud_module_list as v) {
                const data = [
                    title: v.app.app_name,
                    desc: v.app.app_desc,
                    key: v.app.app_key || '',
                    version: v.version || '',
                    author: v.site_name,
                    type: v.app.app_type,
                    support_app: v.app.support_channel || [],
                    is_download: false,
                    is_local: false,
                    icon: v.app.app_logo,
                    cover: v.app.window_logo[0),
                ];
                data.install_info = install_addon_list[v.app.app_key] || [];
                list[v.app.app_key] = data;
            }
            const online_app_list = array_column(list, 'key');
  }

  /**
   * getLocalAddonCount
   * 对应 PHP: CoreAddonService_core::getLocalAddonCount()
   * 逻辑类型: undefined - undefined
   */
  async getLocalAddonCount() {
    // 基于PHP真实逻辑: getLocalAddonCount
    // PHP原文: $files = get_files_by_dir($this->addon_path);         return count($files);     }      /**      * 获取已安装插件数量      * @param array $where      * @return ...
const files = get_files_by_dir(this.addon_path];
        return count(files);
    }

    /**
     * 获取已安装插件数量
     * @param where
     * @return int
     * @throws DbBusinessException
     */
    async getCount(where = [))
    {

        return this.model.where(where).count(];
    }

    /**
     * 安装的插件分页
     * @param where
     * @return * @throws DbBusinessException
     * @throws DbBusinessException
     */
    async getPage(where)
    {
        const field = 'id, title, key, desc, version, status, icon, create_time, install_time';
        const search_model = this.model.withSearch(.title, where).field(field).order('id desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 插件详情
     * @param int id
     * @return */
    async getInfo(int id)
    {
        return this.model.where([['id', '=', id))).findOrEmpty().toArray(];
    }

    /**
     * 设置插件(安装或更新)
     * @param params
     * @return true
     */
    async set(params)
    {
        const title = params.title;
        const key = params.key;
        const addon = this.model.where([
            ['key', '=', key),
        )).findOrEmpty(];
        const version = params.version;//版本号
        const desc = params.desc;
        const icon = params.icon;
        const data = [
            title: title,
            version: version,
            status: 1,
            desc: desc,
            icon: icon,
            key: key,
            compile: params.compile || [),
            type: params.type,
            support_app: params.support_app || ''
        ];
        if (addon.isEmpty()) {
            data.install_time = time(];
            this.model.create(data);
  }

  /**
   * getCount
   * 对应 PHP: CoreAddonService_core::getCount()
   * 逻辑类型: undefined - undefined
   */
  async getCount(where: any[]) {
    // 基于PHP真实逻辑: getCount
    // PHP原文: return $this->model->where($where)->count();     }      /**      * 安装的插件分页      * @param array $where      * @return array      * @throws DbException ...
return this.model.where(where).count(];
    }

    /**
     * 安装的插件分页
     * @param where
     * @return * @throws DbBusinessException
     * @throws DbBusinessException
     */
    async getPage(where)
    {
        const field = 'id, title, key, desc, version, status, icon, create_time, install_time';
        const search_model = this.model.withSearch(.title, where).field(field).order('id desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 插件详情
     * @param int id
     * @return */
    async getInfo(int id)
    {
        return this.model.where([['id', '=', id))).findOrEmpty().toArray(];
    }

    /**
     * 设置插件(安装或更新)
     * @param params
     * @return true
     */
    async set(params)
    {
        const title = params.title;
        const key = params.key;
        const addon = this.model.where([
            ['key', '=', key),
        )).findOrEmpty(];
        const version = params.version;//版本号
        const desc = params.desc;
        const icon = params.icon;
        const data = [
            title: title,
            version: version,
            status: 1,
            desc: desc,
            icon: icon,
            key: key,
            compile: params.compile || [),
            type: params.type,
            support_app: params.support_app || ''
        ];
        if (addon.isEmpty()) {
            data.install_time = time(];
            this.model.create(data);
  }

  /**
   * getPage
   * 对应 PHP: CoreAddonService_core::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'id, title, key, desc, version, status, icon, create_time, install_time';         $search_model = $this->model->withSearch(['title'], $where)...
const field = 'id, title, key, desc, version, status, icon, create_time, install_time';
        const search_model = this.model.withSearch(.title, where).field(field).order('id desc'];
        return this.pageQuery(search_model);
    }

    /**
     * 插件详情
     * @param int id
     * @return */
    async getInfo(int id)
    {
        return this.model.where([['id', '=', id))).findOrEmpty().toArray(];
    }

    /**
     * 设置插件(安装或更新)
     * @param params
     * @return true
     */
    async set(params)
    {
        const title = params.title;
        const key = params.key;
        const addon = this.model.where([
            ['key', '=', key),
        )).findOrEmpty(];
        const version = params.version;//版本号
        const desc = params.desc;
        const icon = params.icon;
        const data = [
            title: title,
            version: version,
            status: 1,
            desc: desc,
            icon: icon,
            key: key,
            compile: params.compile || [),
            type: params.type,
            support_app: params.support_app || ''
        ];
        if (addon.isEmpty()) {
            data.install_time = time(];
            this.model.create(data);
  }

  /**
   * getInfo
   * 对应 PHP: CoreAddonService_core::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: return $this->model->where([['id', '=', $id]])->findOrEmpty()->toArray();     }      /**      * 设置插件(安装或更新)      * @param array $params      * @return...
return this.model.where([['id', '=', id))).findOrEmpty().toArray(];
    }

    /**
     * 设置插件(安装或更新)
     * @param params
     * @return true
     */
    async set(params)
    {
        const title = params.title;
        const key = params.key;
        const addon = this.model.where([
            ['key', '=', key),
        )).findOrEmpty(];
        const version = params.version;//版本号
        const desc = params.desc;
        const icon = params.icon;
        const data = [
            title: title,
            version: version,
            status: 1,
            desc: desc,
            icon: icon,
            key: key,
            compile: params.compile || [),
            type: params.type,
            support_app: params.support_app || ''
        ];
        if (addon.isEmpty()) {
            data.install_time = time(];
            this.model.create(data);
  }

  /**
   * set
   * 对应 PHP: CoreAddonService_core::set()
   * 逻辑类型: undefined - undefined
   */
  async set(params: any[]) {
    // 基于PHP真实逻辑: set
    // PHP原文: $title = $params['title'];         $key = $params['key'];         $addon = $this->model->where([             ['key', '=', $key],         ])->findOrEmp...
const title = params.title;
        const key = params.key;
        const addon = this.model.where([
            ['key', '=', key),
        )).findOrEmpty(];
        const version = params.version;//版本号
        const desc = params.desc;
        const icon = params.icon;
        const data = [
            title: title,
            version: version,
            status: 1,
            desc: desc,
            icon: icon,
            key: key,
            compile: params.compile || [),
            type: params.type,
            support_app: params.support_app || ''
        ];
        if (addon.isEmpty()) {
            data.install_time = time(];
            this.model.create(data);
  }

  /**
   * getInfoByKey
   * 对应 PHP: CoreAddonService_core::getInfoByKey()
   * 逻辑类型: undefined - undefined
   */
  async getInfoByKey(key: string) {
    // 基于PHP真实逻辑: getInfoByKey
    // PHP原文: return $this->model->where([['key', '=', $key]])->findOrEmpty()->toArray();     }      /**      * 通过插件名删除插件      * @param string $key      * @return t...
return this.model.where([['key', '=', key))).findOrEmpty().toArray(];
    }

    /**
     * 通过插件名删除插件
     * @param string key
     * @return true
     */
    async delByKey(string key)
    {
        this.model.where([['key', '=', key))).delete(];
        return true;
    }

    /**
     * 修改插件状态
     * @param int id
     * @param int status
     * @return true
     */
    async setStatus(int id, int status)
    {
        this.model.where([['id', '=', id))).update([status: status)];
        return true;
    }

    async getAppList()
    {
        return event('addon', [)];
    }

    /**
     * 查询已安装的有效的应用
     * @return */
    async getInstallAddonList()
    {
        const addon_list = this.model.where([['status', '=', AddonDict.ON))).append(.status_name).column('title, icon, key, desc, status, type, support_app', 'key');
        if (!!addon_list)) {
            foreach (addon_list as &data) {
                data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
            }
  }

  /**
   * delByKey
   * 对应 PHP: CoreAddonService_core::delByKey()
   * 逻辑类型: undefined - undefined
   */
  async delByKey(key: string) {
    // 基于PHP真实逻辑: delByKey
    // PHP原文: $this->model->where([['key', '=', $key]])->delete();         return true;     }      /**      * 修改插件状态      * @param int $id      * @param int $status...
this.model.where([['key', '=', key))).delete(];
        return true;
    }

    /**
     * 修改插件状态
     * @param int id
     * @param int status
     * @return true
     */
    async setStatus(int id, int status)
    {
        this.model.where([['id', '=', id))).update([status: status)];
        return true;
    }

    async getAppList()
    {
        return event('addon', [)];
    }

    /**
     * 查询已安装的有效的应用
     * @return */
    async getInstallAddonList()
    {
        const addon_list = this.model.where([['status', '=', AddonDict.ON))).append(.status_name).column('title, icon, key, desc, status, type, support_app', 'key');
        if (!!addon_list)) {
            foreach (addon_list as &data) {
                data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
            }
  }

  /**
   * setStatus
   * 对应 PHP: CoreAddonService_core::setStatus()
   * 逻辑类型: undefined - undefined
   */
  async setStatus(id: number, status: number) {
    // 基于PHP真实逻辑: setStatus
    // PHP原文: $this->model->where([['id', '=', $id]])->update(['status' => $status]);         return true;     }      public function getAppList()     {         ret...
this.model.where([['id', '=', id))).update([status: status)];
        return true;
    }

    async getAppList()
    {
        return event('addon', [)];
    }

    /**
     * 查询已安装的有效的应用
     * @return */
    async getInstallAddonList()
    {
        const addon_list = this.model.where([['status', '=', AddonDict.ON))).append(.status_name).column('title, icon, key, desc, status, type, support_app', 'key');
        if (!!addon_list)) {
            foreach (addon_list as &data) {
                data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
            }
  }

  /**
   * getAppList
   * 对应 PHP: CoreAddonService_core::getAppList()
   * 逻辑类型: undefined - undefined
   */
  async getAppList() {
    // 基于PHP真实逻辑: getAppList
    // PHP原文: return event('addon', []);     }      /**      * 查询已安装的有效的应用      * @return array      */     public function getInstallAddonList()     {         $add...
return event('addon', [)];
    }

    /**
     * 查询已安装的有效的应用
     * @return */
    async getInstallAddonList()
    {
        const addon_list = this.model.where([['status', '=', AddonDict.ON))).append(.status_name).column('title, icon, key, desc, status, type, support_app', 'key');
        if (!!addon_list)) {
            foreach (addon_list as &data) {
                data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
            }
  }

  /**
   * getInstallAddonList
   * 对应 PHP: CoreAddonService_core::getInstallAddonList()
   * 逻辑类型: undefined - undefined
   */
  async getInstallAddonList() {
    // 基于PHP真实逻辑: getInstallAddonList
    // PHP原文: $addon_list = $this->model->where([['status', '=', AddonDict::ON]])->append(['status_name'])->column('title, icon, key, desc, status, type, support_ap...
const addon_list = this.model.where([['status', '=', AddonDict.ON))).append(.status_name).column('title, icon, key, desc, status, type, support_app', 'key');
        if (!!addon_list)) {
            foreach (addon_list as &data) {
                data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
            }
  }

  /**
   * getAddonDevelopList
   * 对应 PHP: CoreAddonService_core::getAddonDevelopList()
   * 逻辑类型: undefined - undefined
   */
  async getAddonDevelopList(search: string) {
    // 基于PHP真实逻辑: getAddonDevelopList
    // PHP原文: $list = [];          $install_addon_list = (new Addon())->append(['status_name', 'type_name'])->column('title, icon, key, desc, status, author, versio...
const list = [];

        const install_addon_list = this.addonService.append(['status_name', 'type_name')).column('title, icon, key, desc, status, author, version, install_time, update_time, cover, type', 'key'];
        const files = get_files_by_dir(this.addon_path);
        if (!!files)) {
            const core_addon_service = this.coreAddonService;
            foreach (files as path) {
                const data = core_addon_service.getAddonConfig(path);
                if (typeof data.key)) {
                    const key = data.key;
                    data.install_info = install_addon_list[key] || [];
                    data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
                    data.cover = is_file(data.cover) ? image_to_base64(data.cover) : '';
                    data.is_download = true;
                    data.type_name = !data.type) ? '' : AddonDict.getType()[data.type] || '';
                    list[key] = data;
                }
            }
  }

  /**
   * getAddonDevelopInfo
   * 对应 PHP: CoreAddonService_core::getAddonDevelopInfo()
   * 逻辑类型: undefined - undefined
   */
  async getAddonDevelopInfo(key: string) {
    // 基于PHP真实逻辑: getAddonDevelopInfo
    // PHP原文: $dir = $this->addon_path . $key . DIRECTORY_SEPARATOR;         if (!is_dir($dir)) return [];         $core_addon_service = new CoreAddonService();    ...
const dir = this.addon_path . key . DIRECTORY_SEPARATOR;
        if (!is_dir(dir)) return [];
        const core_addon_service = this.coreAddonService;

        const data = core_addon_service.getAddonConfig(key);
        if (typeof data.key)) {
            data.icon = is_file(data.icon) ? image_to_base64(data.icon) : '';
            data.cover = is_file(data.cover) ? image_to_base64(data.cover) : '';
            data.type_name = !data.type) ? '' : AddonDict.getType()[data.type] || '';
  }

  /**
   * getIndexAddonLabelList
   * 对应 PHP: CoreAddonService_core::getIndexAddonLabelList()
   * 逻辑类型: undefined - undefined
   */
  async getIndexAddonLabelList() {
    // 基于PHP真实逻辑: getIndexAddonLabelList
    // PHP原文: return (new CoreModuleService())->getIndexModuleLabelList()['data'] ?? [];     }      /**      * 获取首页应用      * @param int $label_id      * @return arr...
return this.coreModuleService.getIndexModuleLabelList().data || [];
    }

    /**
     * 获取首页应用
     * @param int label_id
     * @return */
    async getIndexAddonList(label_id)
    {
        return this.coreModuleService.getIndexModuleList(label_id).data || [];
    }
  }

  /**
   * getIndexAddonList
   * 对应 PHP: CoreAddonService_core::getIndexAddonList()
   * 逻辑类型: undefined - undefined
   */
  async getIndexAddonList(label_id: any) {
    // 基于PHP真实逻辑: getIndexAddonList
    // PHP原文: return (new CoreModuleService())->getIndexModuleList($label_id)['data'] ?? [];     }  }...
return this.coreModuleService.getIndexModuleList(label_id).data || [];
    }
  }
}
